import request from '@/utils/request'

// 设备ID

export const getMachineAllListDiewa = () => {
  return request({
    url: '/parameter/getMachineGroupByMachineTypeId/?machineType=叠瓦',
    method: 'get'
  })
}

// 查询列表
export const queryTableData = (query:any) => {
  return request({
    url: '/report/kpi/machineAlarmFrencyList',
    method: 'get',
    params: query
  })
}

type Obj = Record<string, any>

export const _trendsTitle:any[] = ['小报警率(%)', '小报警数(件)', '小报警时(分)']

const cellStyle = ({ rowIndex, value, colDef, data }: any) => {
  if(rowIndex === 0){
    if(_trendsTitle.includes(value)){
      return {
        background: '#fce4d6'
      }
    } else if(colDef.field !== 'attribute15') {
      return {
        color: 'blue',
        fontWeight: 'bold',
        textDecoration: 'underline',
        cursor: 'pointer'
      }
    }
  }

  if(data.title === '实绩-目标' && Number(value) < 0){
    return {
      color: 'red',
      fontWeight: 'bold'
    }
  }
}

export const columns = [
  { headerName: "title", field: "title", minWidth: 130, cellStyle },
  { headerName: "attribute15", field: "attribute15", minWidth: 55, cellStyle },
  { headerName: "attribute1", field: "attribute1", minWidth: 55, cellStyle },
  { headerName: "attribute2", field: "attribute2", minWidth: 55, cellStyle },
  { headerName: "attribute3", field: "attribute3", minWidth: 55, cellStyle },
  { headerName: "attribute4", field: "attribute4", minWidth: 55, cellStyle },
  { headerName: "attribute5", field: "attribute5", minWidth: 55, cellStyle },
  { headerName: "attribute6", field: "attribute6", minWidth: 55, cellStyle },
  { headerName: "attribute7", field: "attribute7", minWidth: 55, cellStyle },
  { headerName: "attribute8", field: "attribute8", minWidth: 55, cellStyle },
  { headerName: "attribute9", field: "attribute9", minWidth: 55, cellStyle },
  { headerName: "attribute10", field: "attribute10", minWidth: 55, cellStyle },
  { headerName: "attribute11", field: "attribute11", minWidth: 55, cellStyle },
  { headerName: "attribute12", field: "attribute12", minWidth: 55, cellStyle },
  { headerName: "attribute13", field: "attribute13", minWidth: 55, cellStyle },
  { headerName: "attribute14", field: "attribute14", minWidth: 55, cellStyle }
]

export const getTableData = (data:any[]) => {
  let lockMapFirst = new Map([
    [1, ""],
    [2, "目标"],
    [3, "实绩"],
    [4, "实绩-目标"],
    [5, "激光"],
    [6, "印刷"],
    [7, "叠片"]
  ])

  let tableGroup:any = {
    'P': '小报警率(%)',
    'C': '小报警数(件)',
    'T': '小报警时(分)'
  }

  let targetData:Obj = {
  }
  let jump:any = null

  for(let key in data) {
    targetData[tableGroup[key]] = []

    data[key].forEach((item:any, index:number) => {
      if(index === 0){
        if(!jump){ jump = item }
      } else {
        index === 1 ? item.title = tableGroup[key] : item.title = lockMapFirst.get(index)
        targetData[tableGroup[key]].push(item)
      }

    })
  }

  return {
    targetData,
    jump
  }
}

export const chartOptios = (data:any[], idx: number, gropTitle:string, time:any) => {
  let legendData = ['目标', '实绩', '实绩-目标', '激光', '印刷', '叠片']
  let xAxisData:string[] = []

  let _dataPool:Obj = {}
  let _barData = ['激光', '印刷', '叠片']
  let _lineData = ['目标', '实绩']

  let _tempxAxisData = ['attribute15', 'attribute1', 'attribute2', 'attribute3', 'attribute4',
                        'attribute5', 'attribute6', 'attribute7', 'attribute8', 'attribute9',
                        'attribute10', 'attribute11', 'attribute12', 'attribute13', 'attribute14']

  let options:any = {
    title: {
      text: '',
      textStyle: {
        fontSize:'16px',
        fontWeight: 500
      }
      // left: 100
    },
    legend: {
      right: '50',
      data: legendData
    },
    grid: [
      {
        left: '130px',
        right: '4px',
        height: '14%',
        top:'18%'
      },
      {
        left: '130px',
        right: '4px',
        top: '40%',
        // height: '50%',
        bottom: '10px'
      }
    ],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      },
      borderWidth: 1,
      borderColor: '#ccc',
      padding: 10,
      textStyle: {
        color: '#000',
        fontSize: 12
      }
    },
    xAxis: [
      {
        type: 'category',
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false // 不显示坐标轴线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        },
        data: xAxisData // 横坐标的数据
      },
      {
        gridIndex: 1,
        type: 'category',
        data: xAxisData,
        boundaryGap: true,
        axisLine: { onZero: false },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        }
        // min: 'dataMin',
        // max: 'dataMax'
      }
    ],
    yAxis: [
      {
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true,
        min: 0
      },
      {
        gridIndex: 1,
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true
      }
    ],
    series: []
  }

  Object.values(data)[idx].forEach((item:any, index:number) => {
    if(index === 0){
      options.xAxis.forEach((val:any) => {
        val.data = [item.attribute15, item.attribute1, item.attribute2, item.attribute3, item.attribute4,
          item.attribute5, item.attribute6, item.attribute7, item.attribute8, item.attribute9,
          item.attribute10, item.attribute11, item.attribute12, item.attribute13, item.attribute14]
      })

      options.title.text = `${gropTitle}_${item.title}_${time}`

    } else {
      _dataPool[item.title] = _tempxAxisData.map(key => item[key])
    }
  })

  for(let key in _dataPool) {

    if(_barData.includes(key)){
      options.series.push({
        name: key,
        type: 'bar',
        stack: '总量',
        barWidth: '40%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        // label: {
        //   show: true,
        //   position: 'insideBottom'
        // },
        data: _dataPool[key]
      })
    }

    if(_lineData.includes(key)){
      options.series.push({
        name: key,
        type: 'line',
        smooth: true,
        label: {
          show: key === '目标' ? false : true,
          position: 'top'
        },
        lineStyle: {
          width: 1
        },
        symbolSize: 2,
        xAxisIndex: 1,
        yAxisIndex: 1,
        data: _dataPool[key]
      })
    }

    if(key === '实绩-目标'){
      options.series.push({
        name: key,
        type: 'bar',
        barWidth: '40%',
        label: {
          show: true,
          position: 'top'
        },
        itemStyle: {
          color: '#dbdbdb'
        },
        data: _dataPool[key]
      })
    }
  }

  return options
}